#pragma once

#include "../_inc_arc/def_archive.h"
#include "SkinListCtrl.h"
#include "../_inc_app/CtrlEdit.h"

#include "../_inc_util/UtilTime.h"
#include "../_inc/TimeList.h"
#include "../_inc/def_interface.h"
#include "SortedArray.h"
#include "ListMgr.h"

// CWndListTime

#define _NEW_LIST

class CWndListTime : public CWnd
{
	DECLARE_DYNAMIC(CWndListTime)

private:
	CSkinListCtrl		m_ListDate;
	CSkinListCtrl		m_ListHour;
	CSkinListCtrl		m_SkinList;		//minutes
	CFont				m_ListFont;

	CUtilTime			m_UtilTime;

	TCHAR				m_tzTemp[32];

	CSortedArray <CTimeList *, CTimeList *> m_arrPlayList;
	CSortedArray <CTimeList *, CTimeList *> m_arrRecList[MAX_REC_TYPE];

	BOOL m_bRecType[MAX_REC_TYPE];
	BOOL m_bSorted;

	int m_nPlayListStart;
	int m_nPlayListCount;

	time_t				m_tmSelectedDate;
	int					m_nSelectedHour;
	int m_nListDateSelect;
	int m_nListHourSelect;

	int m_nFontHeight;

	int m_nDateWidth;
	int m_nHourWidth;
	int m_nListWidth;
	int m_nLeftGap;
	int m_nRightGap;

	BOOL m_bInitList;
	BOOL m_bChecked;

	CListMgr *m_pListMgr;

	int m_nMode;
	
public:
	CWndListTime();
	virtual ~CWndListTime();

	void SetListMgr(CListMgr *pListMgr)	{ m_pListMgr = pListMgr; };
	void LoadRecList();
	void ReloadRecList();

	void SetHourList(int nSelHour = -1);
	void SetRecList();

	void MergeList();
	void ResetArray();

	void ResetList();
	void UpdateDateIndexCal();

	int SearchIndex(time_t tSearchTime, BOOL bFindNearBehind);
	int binarySearchIndex(time_t tSearchTime, int nStart, int nEnd, BOOL *bFind);
	int linearSearchIndex(time_t tSearchTime, int nStart, int nEnd, BOOL *bFind);

	int GetListItem(int nStartCount);
	int GetSelectedItem();

	void SetCheckAll(int nID);
	void SetUncheckAll(int nID);

	void InitListCtrl();

	BOOL GetCheckStatus() { return m_bChecked; };
	void NotifyCheckStatus(BOOL bChecked);

	void SetSelectRecType(DWORD dwRecType, BOOL bChecked);
	void ArrangeRecList();


	int GetRecIndexNumberFromListNumber(int nListNumber);
	int GetRecIndexNumberAvailable(int nIndexNumber);
	int GetCurrentRecIndexNumber();
	CTimeList *GetRecIndex(int nIndexNumber);
	void SetRecIndex(int nIndexNumber);
	int GetRecIndexSize();

	int GetBackupIndexStart();
	int GetBackupIndexNext(int nIndex, BOOL bChecked = TRUE);
	int GetBackupIndexEnd();
	CTimeList *GetBackupIndex(int nIndex);

	void RefreshLang();

protected:
	afx_msg void OnSize(UINT nType, int cx, int cy);
	afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
	afx_msg void OnShowWindow(BOOL bShow, UINT nStatus);
	afx_msg void OnEndtrackSkinList(NMHDR* pNMHDR, LRESULT* pResult);
	afx_msg void OnPaint();
	afx_msg BOOL OnEraseBkgnd(CDC* pDC);
	afx_msg long OnCtlMessage(UINT wParam, LONG lParam);
	afx_msg long OnPlbMessage(UINT wParam, LONG lParam);
	DECLARE_MESSAGE_MAP()

public:	
	virtual BOOL PreTranslateMessage(MSG* pMsg);
	afx_msg void OnDestroy();
};

